﻿using System;

namespace BLL.Extensions.Filtering
{
    /// <summary>
    /// Properties marked with this attribute will be used by <see
    /// cref="QueryableExtensions.ApplyFilter{T}"/> extension method.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class SearchFilterAttribute : Attribute
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SearchFilterAttribute"/> class.
        /// </summary>
        public SearchFilterAttribute() : this(FilterTypes.Equals)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SearchFilterAttribute"/> class.
        /// </summary>
        /// <param name="filterType">Type of the filter this property represents.</param>
        public SearchFilterAttribute(FilterTypes filterType)
        {
            FilterType = filterType;
        }

        /// <summary>
        /// Gets or sets the type of the filter this property represents.
        /// </summary>
        public FilterTypes FilterType { get; set; }

        /// <summary>
        /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
        /// </summary>
        /// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
        /// <returns>
        /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise,
        /// <c>false</c>.
        /// </returns>
        /// <remarks>Overriden in order to make it work nicely with TypeDescriptor.</remarks>
        public override bool Equals(object obj)
        {
            return obj is SearchFilterAttribute;
        }

        /// <summary>
        /// Returns a hash code for this instance.
        /// </summary>
        /// <returns>
        /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a
        /// hash table.
        /// </returns>
        public override int GetHashCode()
        {
            unchecked
            {
                return (base.GetHashCode() * 397) ^ this.FilterType.GetHashCode();
            }
        }
    }
}